package com.cuitrip.business.reminder.model;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import com.cuitrip.business.reminder.RemindDialogActivity;
import com.cuitrip.push.model.PushMessageModel;
import com.cuitrip.service.R;
import com.cuitrip.util.UIUtil;
import com.cuitrip.util.time.InputTime;
import com.cuitrip.util.time.OutputTime;
import com.cuitrip.util.time.TimeUtil;
import com.lab.utils.ImageHelper;

import java.lang.ref.WeakReference;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ak on 15/11/5.
 */
public class StartingReminderViewHolder implements ReminderViewHolder<PushMessageModel> {



    TextView dateText;

    CircleImageView finderAvatar;

    TextView finderName;

    CircleImageView travellerAvatar;

    TextView travellerName;

    View closeAction;

    WeakReference<Activity> mWeakHostActivity;

    @Override
    public int withLayoutRes() {
        return R.layout.remind_starting_dialog;
    }

    @Override
    public void build(RemindDialogActivity activity) {
        dateText = UIUtil.findTextViewById(activity, R.id.date_text);
        finderAvatar = UIUtil.findCircleImageView(activity, R.id.finder_avatar);
        finderName = UIUtil.findTextViewById(activity, R.id.finder_name);

        travellerAvatar = UIUtil.findCircleImageView(activity, R.id.traveller_avatar);
        travellerName = UIUtil.findTextViewById(activity, R.id.traveller_name);

        closeAction = UIUtil.findImageViewById(activity, R.id.close_button);
        mWeakHostActivity = new WeakReference<Activity>(activity);
        closeAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }

    public void dismiss(){
        Activity activity = mWeakHostActivity.get();
        if (null != activity){
            activity.finish();
        }
    }

    @Override
    public void render(PushMessageModel object) {
        String timeFormat = TimeUtil.formatTime(
                InputTime.build(object.getServiceDate(), TimeUtil.getZeroZone()),
                OutputTime.getInstance(OutputTime.OutputType.Type_DAY, TimeUtil.getZeroZone()));
        dateText.setText(timeFormat);
        String leftNick, rightNick;
        String leftAvatar, rightAvatar;
        if (object.getUserType() == PushMessageModel.TRAVELLER){
            leftNick = object.getTravllerNick();
            leftAvatar = object.getTravllerHeadPic();
            rightAvatar = object.getInsiderHeadPic();
            rightNick = object.getInsiderNick();
        }else{
            leftNick = object.getInsiderNick();
            leftAvatar = object.getInsiderHeadPic();
            rightAvatar = object.getTravllerHeadPic();
            rightNick =  object.getTravllerNick();
        }

        finderName.setText(leftNick);
        travellerName.setText(rightNick);
        ImageHelper.displayPersonImage(rightAvatar, travellerAvatar, null);
        ImageHelper.displayPersonImage(leftAvatar,finderAvatar,null);
    }

}
